//
//  SLStasusOriginalView.swift
//  0319iOSweiboDemo
//
//  Created by Tang on 16/3/25.
//  Copyright © 2016年 大天朝. All rights reserved.
//

import UIKit
import SnapKit

//原创微博的view
class SLStasusOriginalView: UIView {
    
    //MARK: - 属性
    //底部约束的属性
    var orginalViewBottomCons : Constraint?
    
    var status : SLStatus? {
        didSet {
            //1.设置头像
            //当别人来设置微博模型的时候.我们根据模型来设置原创微博的相关内容
            if let imageURL = status?.user?.profile_image_url {
                let url = NSURL(string: imageURL)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            //2.设置名称
            nameLabel.text = status?.user?.screen_name
            
            //设置时间
            timeLabel.text = NSDate.dateWithSinaString(status?.created_at ?? "刚刚").sinaDescripiton()
            
            //来源
            sourceLabel.text = status?.source
            
            //微博模型提供的数据不是很合理.需要view显示的时候来计算图片.如果其他地方也需要设置会员等级
            //会员等级
            memberView.image = status?.user?.memberImage
            
            //认证图标
            verifiedView.image = status?.user?.verfiedImage
            
            //微博内容
            contentLabel.text = status?.text
            
            //在设置约束之前删除这个约束
            orginalViewBottomCons?.uninstall() // 删除这个约束
            
            //判断微博是否要显示图片.要显示才显示SLStatusPictureView.不是就隐藏
            //判断是否要显示图片.先判断可选是否有值.有就还要判断数量是否>0
            
            if let pictreURLs = status?.storePictureURLs {
                //有图片就显示
                picutreView.hidden = false
                
                //给picutreView赋值pictreURLs数组
                picutreView.pictureURLs = pictreURLs
                
                //需要修改约束.不然会一直参考picutreView的约束
                //有图片就需要显示SLStasusOriginalView的底部就参照picutreView的底部
                //使用snp_updateConstraints不能更新参照
                self.snp_makeConstraints(closure: { (make) -> Void in
                  self.orginalViewBottomCons = make.bottom.equalTo(picutreView).offset(SLStatusCellMargin).constraint
                })
                
            } else {
                //没有图片就隐藏
                picutreView.hidden = true
                //没有图片就需要显示SLStasusOriginalView的底部就参照contentLabel的底部
                self.snp_makeConstraints(closure: { (make) -> Void in
                   self.orginalViewBottomCons = make.bottom.equalTo(contentLabel).offset(SLStatusCellMargin).constraint
                })
            }
        }
    }
 
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        prepareUI()
    }
    
    private func prepareUI() {
        //添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(picutreView)
        
        //添加约束
        //用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(SLStatusCellMargin)
            make.top.equalTo(self).offset(SLStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        //用户名字
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(SLStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        //发表时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(SLStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(SLStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        //会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(SLStatusCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        //认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        //微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(SLStatusCellMargin)
            make.leading.equalTo(self.snp_leading).offset(SLStatusCellMargin)
            make.trailing.equalTo(self.snp_trailing).offset(-SLStatusCellMargin)
        }
        
        //配图
        picutreView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(SLStatusCellMargin)
            make.leading.equalTo(self).offset(SLStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        //原创SLStasusOriginalView的底部参照picutreView的底部
        self.snp_makeConstraints { (make) -> Void in
           //constraint是这行代码所创建的约束
             self.orginalViewBottomCons = make.bottom.equalTo(picutreView).offset(SLStatusCellMargin).constraint
        }
        
    }
    
    //MARK: - 懒加载
    //用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    //用户名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        
        return label
    }()
    
    //时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(9)
        
        return label
    }()
    
    //来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(9)
        
        return label
    }()
    
    //会员等级
    private lazy var memberView: UIImageView = UIImageView()
    
    //认证图标
    private lazy var verifiedView: UIImageView = UIImageView()
    
    //原创微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        //换行(折行)的宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * SLStatusCellMargin
        return label
    }()
    
    //配图
    private lazy var picutreView: SLStatusPictureView = SLStatusPictureView()
}
